/* p5_10.c: This program is modified from p5_4 to use wide timer 1 to create a longer delay. */


#include "TM4C123GH6PM.h"

void wtimer1A_delaySec(int ttime);

int main (void)
{
    /* enable clock to GPIOF at clock gating control register */
    SYSCTL->RCGCGPIO |= 0x20;
    /* enable the GPIO pins for the LED (PF3, 2, and 1) as output */
    GPIOF->DIR = 0x0E;
    /* enable the GPIO pins for digital function */
    GPIOF->DEN = 0x0E;
    
    while(1)
    {
        GPIOF->DATA = 8;        /* turn on blue LED */
        wtimer1A_delaySec(2);   /* wtimer1A 2 sec delay */
        GPIOF->DATA = 0;        /* turn off blue LED */
        wtimer1A_delaySec(2);   /* wtimer1A 2 sec delay */
    }
}

/* multiple of second delays using periodic mode */
void wtimer1A_delaySec(int ttime)
{
    SYSCTL->RCGCWTIMER |= 2;        /* enable clock to WTimer Module 1 */
    
    WTIMER1->CTL = 0;               /* disable WTimer before initialization */
    WTIMER1->CFG = 0x04;            /* 32-bit option */
    WTIMER1->TAMR = 0x01;           /* one-shot mode and down-counter */
    WTIMER1->TAILR = 16000000 * ttime - 1; /* WTimer A interval load value reg */
    WTIMER1->ICR = 1;
    WTIMER1->CTL |= 0x01;           /* enable WTimer A after initialization */
   
    while((WTIMER1->RIS & 1) == 0) ;    /* wait till timeout */
}